package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/modules/channel/mapper"
	cmodel "ksd-social-api/modules/channel/model"
	context3 "ksd-social-api/modules/msg/model/context"
	mservice "ksd-social-api/modules/msg/service"
	umapper "ksd-social-api/modules/user/mapper"
	"strconv"
)

type ChannelCodeService struct {
	service.BaseService
}

var channelCodeMapper = mapper.ChannelCodeMapper{}
var userBuyChannelMapper = mapper.ChannelMapper{}
var userMapper = umapper.UserMapper{}
var messageMeService = mservice.MessageMeService{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *ChannelCodeService) DuihuanChannel(code string, channelId uint64, userId uint64, systemId uint64, systemName string) bool {
	channelCode := channelCodeMapper.CountChannelCode(code, channelId, systemId)
	// 如果还没兑换，就开始兑换，生成小册订单，同时删除订单
	if channelCode > 0 {
		// 删除兑换的订单
		channelCodeMapper.UpdateChannelCode(code, channelId, systemId)
		// 开始保存订单
		service.saveUserChannelOrder(code, channelId, userId, systemId, systemName)
		// 兑换成功
		return true
	}
	return false
}

/**
 * 保存用户小册订单
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *ChannelCodeService) saveUserChannelOrder(code string, channelId uint64, userId uint64, systemId uint64, systemName string) {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyChannelMapper.CountUserBuyChannelNo(userId, channelId, systemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		channel, _ := channelMapper.GetById(channelId, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userBuyChannel := cmodel.UserBuyChannel{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userBuyChannel.ChannelId = channelId
		userBuyChannel.Title = channel.Title
		userBuyChannel.Cover = ""

		userBuyChannel.UserId = userId
		userBuyChannel.Username = userVo.Username
		userBuyChannel.Avatar = userVo.Avatar
		userBuyChannel.Uuid = userVo.Uuid
		userBuyChannel.Phone = userVo.Telephone
		userBuyChannel.Address = userVo.Address
		userBuyChannel.Nickname = userVo.Nickname

		userBuyChannel.Code = code
		userBuyChannel.PayMethod = 3
		userBuyChannel.PayMethodName = "兑换码"
		userBuyChannel.Tradeno = OutTradeNo
		userBuyChannel.Orderno = OutTradeNo
		userBuyChannel.OrderJson = ""
		userBuyChannel.SystemId = systemId
		userBuyChannel.SystemName = systemName
		userBuyChannel.Price = channel.Price
		// 保存订单的接口
		userBuyChannelMapper.SaveUserBuyChannel(userBuyChannel)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = channel.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com" + channel.Url
		messageMeService.SaveMessageMeBuyResumeTemplate(&messageMeContext)
	}
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *ChannelCodeService) DuihuanCoinChannel(channelId uint64, userId uint64, systemId uint64, systemName string) bool {
	// 根据小册id查询小册信息
	channel, _ := channelMapper.GetById(channelId, systemId)
	userVo := userMapper.GetUserByID(userId, systemId)
	price, _ := strconv.Atoi(channel.Price)
	if userVo.Coin <= 0 || price > userVo.Coin {
		return false
	}
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyChannelMapper.CountUserBuyChannelNo(userId, channelId, systemId)
	if isBuyFlag == 0 {
		userBuyChannel := cmodel.UserBuyChannel{}
		OutTradeNo := service.GetSnowWorkerIdString(10)
		// 处理返回结果
		userBuyChannel.ChannelId = channelId
		userBuyChannel.Title = channel.Title
		userBuyChannel.Cover = ""
		userBuyChannel.UserId = userId
		userBuyChannel.Username = userVo.Username
		userBuyChannel.Avatar = userVo.Avatar
		userBuyChannel.Uuid = userVo.Uuid
		userBuyChannel.Phone = userVo.Telephone
		userBuyChannel.Address = userVo.Address
		userBuyChannel.Nickname = userVo.Nickname

		userBuyChannel.Code = ""
		userBuyChannel.PayMethod = 4
		userBuyChannel.PayMethodName = "学习币"
		userBuyChannel.Tradeno = OutTradeNo
		userBuyChannel.Orderno = OutTradeNo
		userBuyChannel.OrderJson = ""
		userBuyChannel.SystemId = systemId
		userBuyChannel.SystemName = systemName
		userBuyChannel.Price = channel.Price
		// 保存订单的接口
		userBuyChannelMapper.SaveUserBuyChannel(userBuyChannel)
	}
	// 兑换成功
	return true
}
